﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace CochonMVVM
{
    /// <summary>
    /// Delegate Command use to ICommand binding
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DelegateCommand<T> : ICommand
    {
        #region Attributes

        Action<T> executeMethod;
        Func<T, bool> canExecuteMethod;

        #endregion

        #region Execute

        public void Execute(object parameter)
        {
            executeMethod((T)parameter);
        }

        #endregion

        #region CanExecute

        public bool CanExecute(object parameter)
        {
            return canExecuteMethod != null ? canExecuteMethod((T)parameter) : true;
        }
        #endregion

        #region CanExecuteChanged

        public event EventHandler CanExecuteChanged;

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }

        #endregion

        #region Constructors

        public DelegateCommand(Action<T> executeMethod)
        {
            this.executeMethod = executeMethod;
        }

        public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
        {
            this.executeMethod = executeMethod;
            this.canExecuteMethod = canExecuteMethod;
        }

        #endregion
    }
}
